/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.punto;

import java.util.ArrayList;
import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.VImgPOI;
import org.grupoavatar.guiaubicua.VInicial;
import org.grupoavatar.guiaubicua.auxiliares.HiloConsultaRating;
import org.grupoavatar.guiaubicua.auxiliares.HiloRating;
import org.grupoavatar.guiaubicua.auxiliares.Red;
import org.grupoavatar.guiaubicua.auxiliares.Usuario;
import org.grupoavatar.guiaubicua.entidades.ARMarker;
import org.grupoavatar.guiaubicua.mix.MixContext;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import com.handlerexploit.prime.utils.ImageManager;
import com.handlerexploit.prime.utils.ImageManager.ExtendedRequest;

public class TabGeneral extends MasterActivity {

	private ArrayList<String> imagenes;
	private TabGeneral context;
	//private int poi_rating;
	public RatingBar rtgValoracion;
	public TextView txtRating;	
	private int poi_rating;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.tab_general);		
		btnGeneral.setSelected(true);
		btnGeneral.setClickable(false);

		btnUbicacion.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(TabGeneral.this, TabUbicacion.class);
				pasarDatos();
				startActivity(intent);
				overridePendingTransition(0, 0);
				finish();
				overridePendingTransition(0, 0);
			}
		});

		btnServicios.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(TabGeneral.this, TabServicios.class);
				pasarDatos();
				startActivity(intent);
				overridePendingTransition(0, 0);
				finish();
				overridePendingTransition(0, 0);
			}
		});

		boolean mostrarLayout = false;
		
		//Cabecera
		
//		TextView lblNombre = (TextView) findViewById(R.id.lblNombre);
//		lblNombre.setText(poi.getTitle());
//		TextView lblCategoria = (TextView) findViewById(R.id.lblCategoria);
//		lblCategoria.setText(MixContext.getDataSourceName(poi.getDatasource()));
//		
//		ImageView imageCate = (ImageView) this.findViewById(R.id.imgCategoria);
//        Bitmap bitfile_cate = ((ArpucpPOIMarker)poi).getDataSourceCircleIconRA(this.getResources());                
//        imageCate.setImageBitmap(bitfile_cate);  
		
        
		
		// imagenes
		imagenes = marker.getPOI().getUrlImgs();
		if ((imagenes != null) && (imagenes.size() > 0)) {
			mostrarLayout = true;
			int tam = imagenes.size();
			for (int i = 0; i < tam; i++) {
				final String imageURL = imagenes.get(i);

				LayoutInflater inflater = (LayoutInflater) VInicial
						.getContext().getSystemService(
								Context.LAYOUT_INFLATER_SERVICE);
				final LinearLayout linImg = new LinearLayout(
						VInicial.getContext());
				inflater.inflate(R.layout.img_poi, linImg);

				ARPUCPRemoteImageView remoteImageView = new ARPUCPRemoteImageView(
						VInicial.getContext(), linImg);

				remoteImageView.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, 125));
				remoteImageView.setScaleType(ScaleType.CENTER_INSIDE);
				remoteImageView.setAdjustViewBounds(true);
				// padding right a todas menos la ultima imagen
				if (i != tam - 1)
					remoteImageView.setPadding(0, 0, 6, 0);
				remoteImageView.setId(i);

				remoteImageView.setImageURL(imageURL);

				remoteImageView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent i = new Intent(VInicial.getContext(),
								VImgPOI.class);
						i.putExtra("urlImg", imageURL);
						startActivity(i);
					}

				});

				linImg.addView(remoteImageView);

				LinearLayout linlayPoiImgs = (LinearLayout) findViewById(R.id.linlayPoiImgs);
				linlayPoiImgs.addView(linImg);
			}
		} 

		if (!mostrarLayout) {
			HorizontalScrollView hsvImgs = (HorizontalScrollView) findViewById(R.id.hsvImgs);
			hsvImgs.setVisibility(View.GONE);
		} else {
			mostrarLayout = false;
		}

		/*
		 * // titulo TextView TVTitle = (TextView) findViewById(R.id.txtTitle);
		 * TVTitle.setText(poi.getTitle());
		 * 
		 * ImageView imgIcon = (ImageView) findViewById(R.id.imgIconPOI);
		 * imgIcon.setImageResource(poi.getIcon());
		 * 
		 * // LinearLayout linLayPoiTitle = (LinearLayout) //
		 * findViewById(R.id.linLayPoiTitle); // agregarLinea(linLayPoiTitle);
		 */

		// redes sociales
		TextView txtRedes = (TextView) findViewById(R.id.txtRedesSociales);
		txtRedes.setVisibility(ImageView.GONE);
		ImageView imageFB = (ImageView) this.findViewById(R.id.img_fb);
		ImageView imageTW = (ImageView) this.findViewById(R.id.img_tw);
		ImageView imageYT = (ImageView) this.findViewById(R.id.img_yt);
		ImageView imageFS = (ImageView) this.findViewById(R.id.img_fs);
		ImageView imageGP = (ImageView) this.findViewById(R.id.img_gp);		
		imageFB.setVisibility(ImageView.GONE);
		imageTW.setVisibility(ImageView.GONE);
		imageYT.setVisibility(ImageView.GONE);
		imageFS.setVisibility(ImageView.GONE);
		imageGP.setVisibility(ImageView.GONE);
		//separadorRedes.setVisibility(ImageView.GONE);

		ArrayList<Red> redes = marker.getPOI().getRedes();
		for (final Red red : redes) {
			if (red.tieneFB()) {
				imageFB.setVisibility(ImageView.VISIBLE);
				imageFB.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
//						startActivity(Red.getOpenFacebookIntent(
//								VInicial.getContext(), red.getUrl()));
						Red.getOpenFacebookIntent(context, red.getUrl());
					}
				});
				//separadorRedes.setVisibility(ImageView.INVISIBLE);
				txtRedes.setVisibility(ImageView.VISIBLE);
			}
			if (red.tieneTW()) {
				imageTW.setVisibility(ImageView.VISIBLE);
				imageTW.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.setAction(Intent.ACTION_VIEW);
						intent.addCategory(Intent.CATEGORY_BROWSABLE);
						intent.setData(Uri.parse(red.getUrl()));
						startActivity(intent);
					}
				});
				//separadorRedes.setVisibility(ImageView.INVISIBLE);
				txtRedes.setVisibility(ImageView.VISIBLE);
			}
			if (red.tieneYT()) {
				imageYT.setVisibility(ImageView.VISIBLE);
				imageYT.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.setAction(Intent.ACTION_VIEW);
						intent.addCategory(Intent.CATEGORY_BROWSABLE);
						intent.setData(Uri.parse(red.getUrl()));
						startActivity(intent);
					}
				});
				//separadorRedes.setVisibility(ImageView.INVISIBLE);
				txtRedes.setVisibility(ImageView.VISIBLE);
			}
			// NUEVAS
			if (red.tieneFS()) {
				imageFS.setVisibility(ImageView.VISIBLE);
				imageFS.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						startActivity(Red.getOpenFoursquareIntent(
								VInicial.getContext(), red.getUrl()));
					}
				});
				//separadorRedes.setVisibility(ImageView.INVISIBLE);
				txtRedes.setVisibility(ImageView.VISIBLE);
			}
			if (red.tieneGP()) {
				imageGP.setVisibility(ImageView.VISIBLE);
				imageGP.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.setAction(Intent.ACTION_VIEW);
						intent.addCategory(Intent.CATEGORY_BROWSABLE);
						intent.setData(Uri.parse(red.getUrl()));
						startActivity(intent);
					}
				});
				//separadorRedes.setVisibility(ImageView.INVISIBLE);
				txtRedes.setVisibility(ImageView.VISIBLE);
			}
		}

		// valoracion
		rtgValoracion = (RatingBar) findViewById(R.id.rtgValoracion);
		rtgValoracion.setStepSize(0.1f); //				
		TextView txtValoracion = (TextView) findViewById(R.id.txtValoracion);
		txtRating = (TextView) findViewById(R.id.txtRating);		
		if (!((ARMarker) marker).tieneValoracion()) {
			//No tiene valoracion			
			rtgValoracion.setVisibility(RatingBar.GONE);
			
			txtValoracion.setVisibility(TextView.GONE);
			txtRating.setVisibility(TextView.GONE);
		} else {
			//tiene valoracion		
			rtgValoracion.setRating(0);
			txtRating.setText("0/5");
		}
		addListenerOnRatingBar(rtgValoracion);

		// descripción
		String descripcion = marker.getDes_Corta();		
		TextView TituloDescripcion = (TextView) findViewById(R.id.ttlDescripcion);
		
		LinearLayout linDescripcion = (LinearLayout) findViewById(R.id.linLayDescripcion);
		/*
		if (!("".equals(descripcion))) {
			txtDescripcion.setText(descripcion);
		} */

		if (!("".equals(descripcion))) {
//			txtDescripcion.setText(descripcion);
			ArrayList<String> listatexto_data = new ArrayList<String>();
			listatexto_data.add(descripcion);
			establecerBloqueContenidoTexto(linDescripcion, null, listatexto_data, false);
		} else {
			TituloDescripcion.setVisibility(TextView.GONE);							
		}
		ImageButton plus = (ImageButton) this.findViewById(R.id.plusbtn);
		if (!("".equals(marker.getDescripcion()))) {
			Drawable d = VInicial.getContext().getResources().getDrawable(R.drawable.plus);
			int minH = d.getMinimumHeight();
			int minW = d.getMinimumWidth();
			plus.getLayoutParams().height = minH;
			plus.getLayoutParams().width = minW;
			plus.setBackgroundDrawable(d);
			
			plus.setOnClickListener(new Button.OnClickListener() {      
	            public void onClick(View view) {            	
					if (context != null) {
						Dialog dialogPF = new Dialog(context);
						dialogPF.requestWindowFeature(Window.FEATURE_NO_TITLE);
						dialogPF.setContentView(R.layout.detalle_servicios);
						
						ImageView img = (ImageView)dialogPF.findViewById(R.id.img_icono);
						TextView txtTitulo = (TextView)dialogPF.findViewById(R.id.txt_titulo_servicio);
						TextView txtDetalle = (TextView)dialogPF.findViewById(R.id.txt_detalle_servicio);
											
						img.setImageResource(R.drawable.logo_ubicua_peque_nt);
						txtTitulo.setText("Más detalles");
						txtDetalle.setText(agregarSaltosLinea(marker.getDescripcion()));
						dialogPF.show();					
					}
	            }
			});
		} 
		else
		{
			plus.setVisibility(ImageButton.GONE);
		}
		
		// enlaces

		TextView TituloEnlaces = (TextView) findViewById(R.id.ttlEnlaces);		
		
//		LayoutInflater inflater = (LayoutInflater) VInicial.getContext()
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ArrayList<String> enlaces = marker.getURL();
		LinearLayout linEnlaces = (LinearLayout) findViewById(R.id.layoutEnlaces);

		if ((enlaces != null) && (enlaces.size() > 0)) {
			mostrarLayout = true;
			establecerBloqueContenidoTexto(linEnlaces, null, enlaces, true);
			//agregarLinea(linEnlaces);
//			for (int i = 0; i < enlaces.size(); i++) {
//				LinearLayout linText = new LinearLayout(VInicial.getContext());
//				inflater.inflate(R.layout.contentpoi_textlink, linText);
//
//				TextView tvContentText = (TextView) linText
//						.findViewById(R.id.txtPoiContentText);
//				String texto = enlaces.get(i);
//				texto = agregarSaltosLinea(texto);
//
//				int linkColor = this.getResources()
//						.getColor(R.color.link_color);
//				tvContentText.setLinkTextColor(linkColor);
//				tvContentText.setText(Html.fromHtml(texto));
//				tvContentText.setMovementMethod(LinkMovementMethod
//						.getInstance());
//
//				linEnlaces.addView(linText);
//			}
		} else {
			TituloEnlaces.setVisibility(TextView.GONE);						
		}

		if (!mostrarLayout) {
			linEnlaces.setVisibility(View.GONE);
		} else {
			mostrarLayout = false;
		}

		// contacto
		String contacto = marker.getContacto();		
		TextView TituloContacto = (TextView) findViewById(R.id.ttlContacto);
		LinearLayout linContacto = (LinearLayout) findViewById(R.id.linLayContacto);		
		if (!("".equals(contacto))) {
			mostrarLayout = true;			
			//txtContacto.setText(Html.fromHtml(contacto));
			ArrayList<String> listatexto_data = new ArrayList<String>();
			listatexto_data.add(contacto);
			establecerBloqueContenidoTexto(linContacto, null, listatexto_data, false);
//			txtContacto.setText(contacto);
		} else
		{			
			TituloContacto.setVisibility(TextView.GONE);			
		}
		if (mostrarLayout) {
			mostrarLayout = false;
		} 
		
		//horario		
		TextView TituloHorario = (TextView) findViewById(R.id.ttlHorario);
		//ArrayList<String> fechas = poi.getFecha();
		LinearLayout linFechas = (LinearLayout) findViewById(R.id.linLayFecha);		
				
		ArrayList<String> fechas = marker.getFecha();	
//		if ((fechas != null) && (fechas.size() > 0)) {
//			String aux = "";
//			for(int i = 0; i<=fechas.size() -1;i++) {
//				if (i==fechas.size()-1) {
//					aux = aux + fechas.get(i);
//				} else {
//					aux = aux + fechas.get(i)+"\n";
//				}				
//			}
//			txtHorario.setText(aux);
//		}
		if ((fechas != null) && (fechas.size() > 0)) {
			mostrarLayout = true;			
			establecerBloqueContenidoTexto(linFechas, null, fechas, false);			
		}
		else {			
			TituloHorario.setVisibility(TextView.GONE);			
		}	

		/*
		 * // lugares: LinearLayout linContiene =
		 * (LinearLayout).findViewById(R.id.linLayLugares); if (((poiTitleHijos
		 * != null) && (poiTitleHijos.size() > 0)) && ((poiIdHijos != null) &&
		 * (poiIdHijos.size() > 0))) { mostrarLayout = true; int tam =
		 * poiIdHijos.size(); ContenidoListaPOIRow[] listapoi_data = new
		 * ContenidoListaPOIRow[tam]; for (int i = 0; i < tam; i++) {
		 * listapoi_data[i] = new ContenidoListaPOIRow(
		 * obtenerIconSegunId(poiIdHijos.get(i)), poiIdHijos.get(i),
		 * poiTitleHijos.get(i)); } establecerBloqueContenidoPOI(linContiene,
		 * Lugares relacionados", listapoi_data); } if (!mostrarLayout) {
		 * linContiene.setVisibility(View.GONE); } else { mostrarLayout = false;
		 * }
		 * 
		 * 
		 * // ubicacion String ubicacion = poi.getUbicacion(); LinearLayout
		 * linUbicacion = (LinearLayout) findViewById(R.id.linLayUbicacion); if
		 * (!("".equals(ubicacion))) { mostrarLayout = true; ArrayList<String>
		 * listatexto_data = new ArrayList<String>();
		 * listatexto_data.add(ubicacion);
		 * establecerBloqueContenidoTexto(linUbicacion, "Ubicaci�n",
		 * listatexto_data, false); } if (!mostrarLayout) {
		 * linUbicacion.setVisibility(View.GONE); } else { mostrarLayout =
		 * false; }
		 * 
		 * } /* private void establecerBloqueContenidoTexto(LinearLayout
		 * layoutContenedor, String header, ArrayList<String> listatexto_data,
		 * boolean enlaces) { // parametro inflater LayoutInflater inflater =
		 * (LayoutInflater) VInicial.getContext()
		 * .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 * 
		 * if (header != null) { // header LinearLayout linHeader = new
		 * LinearLayout(VInicial.getContext());
		 * inflater.inflate(R.layout.contentpoi_header, linHeader);
		 * 
		 * TextView tvHeader = (TextView) linHeader
		 * .findViewById(R.id.txtContentHeader); tvHeader.setText(header);
		 * 
		 * layoutContenedor.addView(linHeader); }
		 * 
		 * // Contenido if ((listatexto_data != null) && (listatexto_data.size()
		 * != 0)) { for (int i = 0; i < listatexto_data.size(); i++) {
		 * LinearLayout linText = new LinearLayout(VInicial.getContext()); if
		 * (enlaces) { inflater.inflate(R.layout.contentpoi_textlink, linText);
		 * } else { inflater.inflate(R.layout.contentpoi_text, linText); }
		 * TextView tvContentText = (TextView) linText
		 * .findViewById(R.id.txtPoiContentText); String texto =
		 * listatexto_data.get(i); texto = agregarSaltosLinea(texto); if
		 * (enlaces) { int linkColor = this.getResources().getColor(
		 * R.color.link_color); tvContentText.setLinkTextColor(linkColor);
		 * tvContentText.setText(Html.fromHtml(texto));
		 * tvContentText.setMovementMethod(LinkMovementMethod .getInstance()); }
		 * else { tvContentText.setText(texto); Linkify.addLinks(tvContentText,
		 * Linkify.ALL); }
		 * 
		 * layoutContenedor.addView(linText); } }
		 */
		
		new HiloConsultaRating(this).execute();
	}

	private void addListenerOnRatingBar(final RatingBar ratingBar) {
		// TODO Auto-generated method stub
			 
		//if rating value is changed,
		//display the current rating value in the result (textview) automatically		
//		ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
//			public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
//				if (seteo) {
//					Log.d(VInicial.TAG + "-Rating", "El evento se ha disparado por un seteo manual, no se considera");
//					seteo=false;
//				} else {
//					//tira el hilo					
//					Log.d(VInicial.TAG + "-Rating", "El usuario seleccionó un valor de Rating");
//					poi_rating = (int)rating;
//					Log.d(VInicial.TAG + "-Rating", "Se actualizó el valor de poi_rating con el valor dato por el usuario"+poi_rating);
//					hiloActualizadorRating();					
//				}									
//			}
//		});
		
		ratingBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            	if (Usuario.esValido) {
	                if (event.getAction() == MotionEvent.ACTION_UP) {
	                     float touchPositionX = event.getX();
	                     float width = ratingBar.getWidth();
	                     float starsf = (touchPositionX / width) * 5.0f;
	                     int stars = (int)starsf + 1;
	                     ratingBar.setRating(stars);
	                     poi_rating = stars;
	                     hiloActualizadorRating();
	                     //Toast.makeText(context, String.valueOf(stars), Toast.LENGTH_SHORT).show();                   
	                     v.setPressed(false);
	                }
	                if (event.getAction() == MotionEvent.ACTION_DOWN) {
	                    v.setPressed(true);
	                }
	
	                if (event.getAction() == MotionEvent.ACTION_CANCEL) {
	                    v.setPressed(false);
	                }
            	} else {
            		v.setPressed(false);
            	}
                return true;
            }
		});
	}

	protected void hiloActualizadorRating() {
		// TODO Auto-generated method stub						
		new HiloRating(this).execute();
	}	

	private String agregarSaltosLinea(String texto) {
		String txt = texto;
		txt = txt.replace("\\n", "\n");
		return txt;
	}

	class ContenidoListaPOIRow {
		public int icon;
		public String id;
		public String title;

		public ContenidoListaPOIRow() {
			super();
		}

		public ContenidoListaPOIRow(int icon, String id, String title) {
			super();
			this.icon = icon;
			this.id = id;
			this.title = title;
		}
	}

	class ARPUCPRemoteImageView extends ImageView {

		private ImageManager mImageManager;
		private LinearLayout linImg;

		private String mImageURL;

		public ARPUCPRemoteImageView(Context context) {
			super(context);
			setup(context, null, 0);
		}

		public ARPUCPRemoteImageView(Context context, AttributeSet attrs) {
			super(context, attrs);
			setup(context, attrs, 0);
		}

		public ARPUCPRemoteImageView(Context context, AttributeSet attrs,
				int defStyle) {
			super(context, attrs, defStyle);
			setup(context, attrs, defStyle);
		}

		public ARPUCPRemoteImageView(MixContext context, LinearLayout linImg) {
			super(context);
			setup(context, null, 0);
			this.linImg = linImg;
		}

		private void setup(Context context, AttributeSet attrs, int defStyle) {
			mImageManager = ImageManager.getInstance(context);
		}

		/**
		 * Sets the content of this ImageView to the specified URL.
		 * 
		 * @param source
		 *            The URL of a remote image
		 */
		public void setImageURL(String source) {
			if (source != null) {
				mImageURL = source;
				mImageManager.get(mExtendedRequest);
			}
		}

		private ExtendedRequest mExtendedRequest = new ExtendedRequest() {

			@Override
			public void onImageReceived(String source, Bitmap bitmap) {
				if (mImageURL != null && mImageURL.equals(source)
						&& bitmap != null) {
					setImageBitmap(bitmap);
					ProgressBar p = (ProgressBar) linImg
							.findViewById(R.id.pgbDownloadingPoiImage);
					p.setVisibility(View.GONE);
				}
			}

			@Override
			public String getSource() {
				return mImageURL;
			}

			@Override
			public Bitmap onPreProcess(Bitmap raw) {
				return raw;
			}

			@Override
			public int getHeight() {
				return ARPUCPRemoteImageView.this.getHeight();
			}

			@Override
			public int getWidth() {
				return ARPUCPRemoteImageView.this.getWidth();
			}
		};
	}
	
	public int getRating() {
		return poi_rating;
	}

	public void actualizar_rating() {
		// TODO Auto-generated method stub
		try {
//			float promedio =((ArpucpPOIMarker) poi).PromStars();
			float promedio = marker.getPromedio();
			rtgValoracion.setRating(promedio);
			
			String textoRating = promedio+"/5";
			
			if (resultado != null) {
				textoRating = textoRating + "\t\tTotal de votos: " + resultado.getNumeroVotos();
				int votoUsuario = resultado.getVotoUsuario(); 
				if (votoUsuario > 0)
					textoRating = textoRating + "\t\tTu voto: " + votoUsuario;
			}
			
			txtRating.setText(textoRating);
			txtRating.invalidate();//fuerza el redraw
			Log.d(VInicial.TAG + "-Rating", "Se recalculan las estrellas ");
			Log.d(VInicial.TAG + "-Rating", "Se setean en "+promedio);
		} catch (Exception e) {
			Log.d(VInicial.TAG + "-Rating", "Hubo un error en el seteo");
		} finally {
			//seteo = true;
		}
	}
	
	private void establecerBloqueContenidoTexto(LinearLayout layoutContenedor, String header, ArrayList<String> listatexto_data, boolean enlaces) {
		//parametro inflater
		LayoutInflater inflater = (LayoutInflater)VInicial.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (header != null)	{
			//header
			LinearLayout linHeader = new LinearLayout(VInicial.getContext());
			inflater.inflate(R.layout.contentpoi_header, linHeader);
			
			TextView tvHeader = (TextView) linHeader.findViewById(R.id.txtContentHeader);
			tvHeader.setText(header);
			
			layoutContenedor.addView(linHeader);
		}
		
		//Contenido
		if ( (listatexto_data != null) && (listatexto_data.size()!=0) )	{
			for (int i = 0; i < listatexto_data.size(); i++) {
				LinearLayout linText = new LinearLayout(VInicial.getContext());
				if (enlaces)	{
					inflater.inflate(R.layout.contentpoi_textlink, linText);
				}
				else	{
					inflater.inflate(R.layout.contentpoi_text, linText);
				}
				TextView tvContentText = (TextView) linText.findViewById(R.id.txtPoiContentText);
				String texto = listatexto_data.get(i);
				texto = agregarSaltosLinea(texto);
				if (enlaces)	{
					int linkColor = this.getResources().getColor(R.color.link_color);
					tvContentText.setLinkTextColor(linkColor);
					tvContentText.setText(Html.fromHtml(texto));
					tvContentText.setMovementMethod(LinkMovementMethod.getInstance());
				}
				else	{
					tvContentText.setText(texto);
					Linkify.addLinks(tvContentText, Linkify.ALL);
				}

				layoutContenedor.addView(linText);
			}
		}		
	}
		
}
